using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;
using RSSApplicationUpdater;
using pigeon;
using HydroMultyUser;
using WareForms;
//using PluginHundler;
using Hydrobase;
using HydrobaseSDK;
using wow_backtiupCore;
using wow_backitupSDK;
using System.Diagnostics;



namespace wow_backtiup
{
    /// <summary>
    /// Main window of the project
    /// </summary>
    public partial class frmGui : WareForm
    { 
        /// <summary>
        /// instance of projects kernel
        /// </summary>
        Kernel kernel = new Kernel();
        ApplicationUpdater updater;
        /// <summary>
        /// instance of the  core 's feedback library
        /// </summary>
        pigCore pigCore = new pigCore();
        /// <summary>
        /// instance of the  window feedback library
        /// </summary>
        pigMessageForm pigForm = new pigMessageForm();

        hydrobaseADO HBClassADO = new hydrobaseADO();
        /// <summary>
        /// cells of the options file
        /// </summary>
        internal static string[] GenOptionsCells = { BaseClass.FileTagPrimKey + "Default_World_Of_Warcraft_Folder", BaseClass.FileTagPrimKey + "Default_Backup_Folder", BaseClass.FileTagPrimKey + "Default_Machine_Name", BaseClass.FileTagPrimKey + "ExecuteExternalAppsOnExit", BaseClass.FileTagPrimKey + "HideExternalAppToolbar" };
        /// <summary>
        /// boolean variable that controls if external app will be executed when this project will close
        /// </summary>
        public static Boolean ExecuteExternalAppsOnClose;
        /// <summary>
        ///  boolean variable that controls if external app's toolbar will be shown or not
        /// </summary>

        public static Boolean HideExternalAppsToolbar;
        
        PluginHundler pluginHundel;
    //  static public wow_backitupSDK.PluginCollection Loaded_plugins;
        public frmGui()
        {
            InitializeComponent();
            pluginHundel = new PluginHundler();
            HydroMultyUserCore multyusrcore = new HydroMultyUserCore();
            string customdir =Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData) + "\\"+ Application.ProductName;
            multyusrcore.initialiseMultyUserInterface(this, false, customdir);
            optionsCore.OptGeneralTags = GenOptionsCells;
            //Kernel.Appinfocells = Kernel.Appinfocells;
            optionsCore.CreateSettingsFiles();
           // optionsCore.LoadSettings();
           
            
        }

        private void frmGui_Load(object sender, EventArgs e)
        {

            try
            {

                this.Text = Application.ProductName + " -  Core Version:" + Kernel.GetVersion() + "WoW BackitUp SDK Version :"
                    + SDKCore.GetSDKversion();
             /* updater = new ApplicationUpdater(true);
                updater.GetExeCutingApplicationVersion(Assembly.LoadFile(Application.ExecutablePath));
                
                if (updater.properties.AutoCheck == true)
                {
                    
                   updater.CheckforNewVersionsChangeLog();
                } 
                */
               // SDKCore.LoadedPlugins = frmGui.Loaded_plugins;
                if ((optionsCore.GeneralSet != null) || (optionsCore.GeneralSet.Tables[0] != null) || (optionsCore.GeneralSet.Tables[0].Rows[0] != null))
                {
                    object[] vals = optionsCore.GeneralSet.Tables[0].Rows[0].ItemArray;
                    this.txtWowFolder.Text = Convert.ToString(vals[0]);

                    this.txtBackupFolder.Text = Convert.ToString(vals[1]);
                    this.txtMachinename.Text = Convert.ToString(vals[2]);
                    if ((vals[3] != null) || (Convert.ToString(vals[3]) != "-") || (vals[3] != DBNull.Value))
                    {
                        ExecuteExternalAppsOnClose = Convert.ToBoolean(vals[3]);
                    }
                    else
                    {
                        ExecuteExternalAppsOnClose = false;
                    }
                    if ((vals[4] != null) || (Convert.ToString(vals[4]) != "-") || (vals[4] != DBNull.Value))
                    {
                        HideExternalAppsToolbar  = Convert.ToBoolean(vals[4]);
                        if (HideExternalAppsToolbar == true)
                        {

                         //   this.tspExternalApps.Visible = false;
                        } 
                    }
                    else
                    {
                      //  HideExternalAppsToolbar = false;
                      //  this.tspExternalApps.Visible = false;
                    }
                }
                this.notifyIcon.Icon = this.Icon;
                this.notifyIcon.Text = this.Text;
            }

            catch (Exception ex)
            {
                Program.Bugtracking(ex);

            }
            


        }

        public void ExecutePlugins()
        {

            try
            {
                string tmp;

               
                if (SDKCore.LoadedPlugins  != null)
                {
                    //MessageBox.Show("hi");

                    foreach (wow_backitupSDK.GeneralPluginSDK plugin in SDKCore.LoadedPlugins)
                    {
                        wow_backitupSDK.PluginContent context = new wow_backitupSDK.PluginContent();
                       
                        plugin.MakeAction(context, this);
                       
                        tmp = Convert.ToString(this.Tag);
                      
                    }
                   

                }
            }




            catch (Exception e)
            {
                Program.Bugtracking(e);

            }



        }
        protected override void OnLoad(EventArgs e)
        {
            try
            {
                string tmp, tmp2;
                char[] tmchar;
              


                string[] infoparam = new string[2];
               
                optionsCore.OptGeneralTags = GenOptionsCells;
               optionsCore.settingDir = SDKBase.ApplicationPreferencesDir;


            
                optionsCore.LoadSettings();
                if ((optionsCore.GeneralSet != null) || (optionsCore.GeneralSet.Tables != null) || (optionsCore.AppinfoSet != null) || (optionsCore.AppinfoSet.Tables != null) || (optionsCore.GeneralSet.Tables[0] != null) || (optionsCore.AppinfoSet.Tables[0] != null))
                {





                    LoadPlugins();

                    tmp = Convert.ToString(this.Tag);




                    optionsCore.LoadSettings();
                    kernel.AddExteranlAppToold(tspExternalApps, optionsCore.AppinfoSet.Tables[0], externalApplicationsToolStripMenuItem);


                    base.OnLoad(e);

                }
               
            }

            catch (Exception ex)
            {

                Program.Bugtracking(ex);
            }
        




        
        
    
        }
        private void LoadPlugins()
        {
            try
            {

                SDKCore.LoadedPlugins   = (wow_backitupSDK.PluginCollection)ConfigurationSettings.GetConfig("plugins");
             
              //  string[] infoparam = new string[2];
             
                ExecutePlugins();


            }
            catch (Exception e)
            {

                Program.Bugtracking(e);
            }

        }
        private void btnWowBrowse_Click(object sender, EventArgs e)
        {
            try
            {

                if (this.Folders.ShowDialog() == DialogResult.OK)
                {


                    this.txtWowFolder.Text = Folders.SelectedPath;

                }


            }

            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        private void btnBakBrowse_Click(object sender, EventArgs e)
        {
              try
              {
                  
                  if (this.Folders.ShowDialog() == DialogResult.OK)
                {


                    this.txtBackupFolder.Text  = Folders.SelectedPath;

                }


            }

            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        public void btnBackup_Click(object sender, EventArgs e)
        {
            try
            {
                if ((txtBackupFolder.Text !=null)&&(txtWowFolder.Text!=null))
                {
                    kernel.ChangeOriginalFileNameBeforeBackup(txtWowFolder.Text);
                    kernel.BackupWTF(txtWowFolder.Text, txtBackupFolder.Text);
                    Kernel.WTFprog.Hide();

                    kernel.BackupInterface(txtWowFolder.Text, txtBackupFolder.Text);
                    Kernel.IntProg.Hide();
                   
                    kernel.RestoreOriginalFileNameAfterBackup();
                }


            }

            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        
        }

        private void btnRestoreAddons_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog.InitialDirectory = txtBackupFolder.Text;
                if ((openFileDialog.ShowDialog() == DialogResult.OK) || (openFileDialog.FileName != null) ||
                    (this.txtWowFolder.Text!=null) || (this.txtBackupFolder.Text  !=null))
                {
                    openFileDialog.Title += "an Archive with your Inteface (addons) Backup";
                    
                    kernel.RestoreInterface(openFileDialog.FileName , this.txtWowFolder.Text );
                    MessageBox.Show(" Restore Finished!");
                }

            }
            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        private void btnRestoreWTF_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog.InitialDirectory = txtBackupFolder.Text;
                if ((openFileDialog.ShowDialog() == DialogResult.OK) || (openFileDialog.FileName != null) ||
                    (this.txtWowFolder.Text != null) ||(txtBackupFolder.Text !=null))
                {
                    openFileDialog.Title += "an Archive with your Settings (wtf folder) Backup";
                   
                    kernel.ChangeOriginalFilenameBeforeRestore(this.txtMachinename.Text, this.txtWowFolder.Text);


                    kernel.RestoreWTF (openFileDialog.FileName , this.txtWowFolder.Text);
                    kernel.ChangeOriginalFilenameAfterRestore(this.txtMachinename.Text, this.txtWowFolder.Text);
                    MessageBox.Show(" Restore Finished!");

                }

            }
            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }

        }

       
        private void btnFeedBack_Click(object sender, EventArgs e)
        {
            try
            {
                pigForm.Show();




            }
            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
            
        }

        private void btnOptions_Click(object sender, EventArgs e)
        {
             
            try
            {
                optionsGUI frmoptions = new optionsGUI();
                frmoptions.Show();

            }
            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            try
            {
                AboutBox about = new wow_backtiup.AboutBox();
                about.Show();

            }
            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                updater.GetExeCutingApplicationVersion(Assembly.LoadFile(Application.ExecutablePath));
               updater.CheckforNewVersionsChangeLog();
                
            }
            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        private void frmGui_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                if (ExecuteExternalAppsOnClose == true)
                {
                    kernel.ExecuteExternalApps(optionsCore.AppinfoSet.Tables[0]);

                }
            }
            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        private void backupAddonsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if ((txtBackupFolder.Text != null) && (txtWowFolder.Text != null))
                {
                    

                    kernel.BackupInterface(txtWowFolder.Text, txtBackupFolder.Text);
                    Kernel.IntProg.Hide();
                  
                }


            }

            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        private void backupSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if ((txtBackupFolder.Text != null) && (txtWowFolder.Text != null))
                {
                    kernel.ChangeOriginalFileNameBeforeBackup(txtWowFolder.Text);
                    kernel.BackupWTF(txtWowFolder.Text, txtBackupFolder.Text);
                    Kernel.WTFprog.Hide();

                    
                    kernel.RestoreOriginalFileNameAfterBackup();
                }


            }

            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
                
            }
            catch (Exception ex)
            {


                Program.Bugtracking(ex);
            }
        }

        private void frmGui_Resize(object sender, EventArgs ev)
        {
            try
            {

                if ((this.WindowState == FormWindowState.Minimized))
                {

                    this.ShowInTaskbar = false;
                    this.WindowState = FormWindowState.Minimized;
                    
                }


            }
            catch (Exception e)
            {
                Program.Bugtracking(e);
                
            }
        }

        private void showHideToolStripMenuItem_Click(object sender, EventArgs ev)
        {
            try
            {
                if ((this.WindowState == FormWindowState.Minimized) || (this.ShowInTaskbar == false))
                {
                    this.ShowInTaskbar = true;

                    
                    this.WindowState = FormWindowState.Normal;




                }
            }
            catch (Exception e)
            {
                Program.Bugtracking(e);

            }
        }

        private void statusInfo_TextChanged(object sender, EventArgs ev)
        {
            try
            {
                this.toolStripStatusText.Text = this.statusInfo.Text;
               // this.Text = this.toolStripStatusText.Text;

            }
            catch (Exception e)
            {
                Program.Bugtracking(e);

            }
        }

        private void btnShowBackupFolder_Click(object sender, EventArgs ev)
        {
            try
            {
                if(txtBackupFolder.Text != string.Empty )
                {
                    Process.Start(txtBackupFolder.Text);

                }

            }
            catch (Exception e)
            {
                Program.Bugtracking(e);

            }
        }
        

      
       
        
        
    }

        
}